package com.puti.doctor.controller;

import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.puti.common.core.constant.HttpStatus;
import com.puti.common.core.domain.R;
import com.puti.doctor.domian.GoodsInfo;
import com.puti.doctor.domian.SeckillEvaluate;
import com.puti.doctor.service.IGoodsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.puti.common.log.annotation.Log;
import com.puti.common.log.enums.BusinessType;
import com.puti.common.security.annotation.PreAuthorize;

import com.puti.common.core.web.controller.BaseController;
import com.puti.common.core.web.domain.AjaxResult;
import com.puti.common.core.utils.poi.ExcelUtil;
import com.puti.common.core.web.page.TableDataInfo;

/**
 * 商品-信息 商品-信息Controller
 *
 * @author puti
 * @date 2021-04-22
 */
@CrossOrigin
@RestController
@RequestMapping("/shop")
public class GoodsInfoController extends BaseController
{
    @Autowired
    private IGoodsInfoService goodsInfoService;

    /**
     * 查询商品-信息 商品-信息列表
     */

    @PreAuthorize(hasPermi = "system:info:list")
    @GetMapping("/list")
    public TableDataInfo list(GoodsInfo goodsInfo)
    {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        return getDataTable(list);
    }

    //商品列表
    @GetMapping("/listtime")
    public TableDataInfo listtime(GoodsInfo goodsInfo)
    {
        startPage();
        List<GoodsInfo> list = goodsInfoService.selectHospitalDoctorInfotime(goodsInfo);
        return getDataTable(list);
    }

    /**
     * 导出商品-信息 商品-信息列表
     */
    @PreAuthorize(hasPermi = "system:info:export")
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsInfo goodsInfo) throws IOException
    {
        List<GoodsInfo> list = goodsInfoService.selectGoodsInfoList(goodsInfo);
        ExcelUtil<GoodsInfo> util = new ExcelUtil<GoodsInfo>(GoodsInfo.class);
        util.exportExcel(response, list, "商品-信息 商品-信息数据");
    }

    /**
     * 获取商品-信息 商品-信息详细信息
     */
//信息详细信息
    @GetMapping("/good")
    public AjaxResult getInfo(Long goodsId)
    {
        return AjaxResult.success(goodsInfoService.selectGoodsInfoById(goodsId));
    }

    /**
     * 新增商品-信息 商品-信息
     */

    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GoodsInfo goodsInfo)
    {
        return toAjax(goodsInfoService.insertGoodsInfo(goodsInfo));
    }

    /**
     * 修改商品-信息 商品-信息
     */
    @PreAuthorize(hasPermi = "system:info:edit")
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GoodsInfo goodsInfo)
    {
        return toAjax(goodsInfoService.updateGoodsInfo(goodsInfo));
    }

    /**
     * 删除商品-信息 商品-信息
     */
    @PreAuthorize(hasPermi = "system:info:remove")
    @Log(title = "商品-信息 商品-信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds)
    {
        return toAjax(goodsInfoService.deleteGoodsInfoByIds(goodsIds));
    }

    /**
     * 秒杀专场-立即选购：进入商品详情页面；
     */
    @GetMapping("puti-code/{goodsCode}")
    public R selectImmediate(@PathVariable("goodsCode") String goodsCode){

        try {
            Map<Object, Object> map = new HashMap<>();

            List<GoodsInfo> s = (List<GoodsInfo>) goodsInfoService.selectImmediate(goodsCode);

            List<SeckillEvaluate> c = (List<SeckillEvaluate>) goodsInfoService.selectseckill(goodsCode);

            map.put("goodsorder",s);
            map.put("seckill",c);
            return R.restResult(map, HttpStatus.SUCCESS,"查询成功");

        }catch (Exception e){
            e.printStackTrace();
            return R.restResult(null,HttpStatus.SUCCESS,"查询失败");
        }
    }


    /**
     * 商品
     */

    @GetMapping("puti-Codes/{goodsCode}")
    public R selectInfo(@PathVariable("goodsCode") String goodsCode){
        try {
            List<GoodsInfo> goodsInfos = goodsInfoService.selectInfo(goodsCode);
            return R.restResult(goodsInfos,HttpStatus.SUCCESS,"查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.restResult(null,HttpStatus.SUCCESS,"查询失败");
        }
    }
    /**
     * 评论
     */

    @GetMapping("puti_evaluat/{goodsCode}")
    public List<SeckillEvaluate> selectEvaluat(String goodsCode){
        return goodsInfoService.selectEvaluat(goodsCode);
    }




}
